/**
 * Copyright (C) Study Information Technology, LTD. 
 * All Rights Reserved.
 *
 * TransactionManagerConfig.java created on 2020年3月8日 下午8:41:10 by Lyon Lu 
 */
package com.mvc.server.config;

import javax.annotation.Resource;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
 * <pre>
 * Description:
 * 
 * @author Lyon Lu
 * @date 2020年3月8日 下午8:41:10
 *
 * </pre>
 */
@Aspect
@Component
public class TransactionManagerConfig {
	
	private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.mvc..service.*.*(..))";
	
	@Resource
    private PlatformTransactionManager transactionManager;
	
	@Bean
    public TransactionInterceptor txAdvice() {

        DefaultTransactionAttribute txAttr_REQUIRED = new DefaultTransactionAttribute();
        txAttr_REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        
        DefaultTransactionAttribute txAttr_SUPPORTS_READONLY = new DefaultTransactionAttribute();
        txAttr_SUPPORTS_READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        txAttr_SUPPORTS_READONLY.setReadOnly(true);

        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        source.addTransactionalMethod("save*", txAttr_REQUIRED);
        source.addTransactionalMethod("create*", txAttr_REQUIRED);
        source.addTransactionalMethod("insert*", txAttr_REQUIRED);
        source.addTransactionalMethod("delete*", txAttr_REQUIRED);
        source.addTransactionalMethod("update*", txAttr_REQUIRED);
        source.addTransactionalMethod("exec*", txAttr_REQUIRED);
        source.addTransactionalMethod("set*", txAttr_REQUIRED);
        source.addTransactionalMethod("batch*", txAttr_REQUIRED);
        source.addTransactionalMethod("get*", txAttr_SUPPORTS_READONLY);
        source.addTransactionalMethod("query*", txAttr_SUPPORTS_READONLY);
        source.addTransactionalMethod("select*", txAttr_SUPPORTS_READONLY);
        source.addTransactionalMethod("find*", txAttr_SUPPORTS_READONLY);
        source.addTransactionalMethod("list*", txAttr_SUPPORTS_READONLY);
        source.addTransactionalMethod("count*", txAttr_SUPPORTS_READONLY);
        source.addTransactionalMethod("is*", txAttr_SUPPORTS_READONLY);

        return new TransactionInterceptor(transactionManager, source);
    }

    @Bean
    public Advisor txAdviceAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice());
    }
}
